package com.lbd.system.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.system.bean.Role;
import com.lbd.system.service.RoleService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by zzl on 2016/9/26.
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    RoleService roleService;

    /**
     * 根据条件分页查询角色信息
     *
     * @param page 第几页
     * @param rows 一页多少条
     * @param role 角色信息
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, Role role) {
        return new Result(ResultState.SUCCESS, "查询成功", roleService.findByPageConditionJpql(page, rows, role));
    }

    /**
     * 保存角色信息
     *
     * @param role
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(Role role) {
        roleService.save(role);
        return new Result(ResultState.SUCCESS, "添加成功", "");
    }


    /**
     * 根据ID查询要编剧的角色信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", roleService.findById(id));
    }

    /**
     * 修改角色信息
     *
     * @param role
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(Role role) {
        roleService.update(role);
        return new Result(ResultState.SUCCESS, "更新成功");
    }

    /**
     * 查询所有角色信息
     *
     * @return
     */
    @RequestMapping(value = "/roleTree")
    public Result tree() {
        List<Role> roleList = roleService.findAll();
        return new Result(ResultState.SUCCESS, "查询成功", roleList);
    }

}
